//
// Created by antler on 2018/12/21.
//

#include "UIContextUtils.h"

static jmethodID methodID_UIContext_createView = nullptr;
static jmethodID methodID_UIContext_show = nullptr;
static jmethodID methodID_UIContext_close = nullptr;
static jmethodID methodID_UIContext_getRootView = nullptr;
static jmethodID methodID_UIContext_findView = nullptr;

namespace UIContextUtils {

    jobject createView(jobject juicontext, const std::string &layout) {
        jobject juiview = nullptr;

        do {
            JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
            if (!env) {
                break;
            }

            if (!juicontext) {
                break;
            }

            if (methodID_UIContext_createView == nullptr) {
                ScopedLocalRef<jclass> localClassUIContext(env, env->GetObjectClass(juicontext));
                if (!localClassUIContext) {
                    break;
                }

                methodID_UIContext_createView = env->GetMethodID(localClassUIContext,
                                                                 METHOD_NAME_UICONTEXT_CREATEVIEW,
                                                                 METHOD_SIG_UICONTEXT_CREATEVIEW);
            }

            if (!methodID_UIContext_createView) {
                assert(0);
                break;
            }

            ScopedLocalRef<jstring> jlayout(env, env->NewStringUTF(layout.c_str()));
            if (!jlayout) {
                break;
            }

            juiview = env->CallObjectMethod(juicontext, methodID_UIContext_createView,
                                            (jstring) jlayout);

        } while (0);
        return juiview;
    }

    void show(jobject juicontext) {
        do {
            JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
            if (!env) {
                break;
            }

            if (!juicontext) {
                break;
            }

            if (methodID_UIContext_show == nullptr) {
                ScopedLocalRef<jclass> localClassUIContext(env, env->GetObjectClass(juicontext));
                if (!localClassUIContext) {
                    break;
                }

                methodID_UIContext_show = env->GetMethodID(localClassUIContext,
                                                           METHOD_NAME_UICONTEXT_SHOW,
                                                           METHOD_SIG_UICONTEXT_SHOW);
            }

            if (!methodID_UIContext_show) {
                assert(0);
                break;
            }

            env->CallVoidMethod(juicontext, methodID_UIContext_show);

        } while (0);
    }

    void close(jobject juicontext) {
        do {
            JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
            if (!env) {
                break;
            }

            if (!juicontext) {
                break;
            }

            if (methodID_UIContext_close == nullptr) {
                ScopedLocalRef<jclass> localClassUIContext(env, env->GetObjectClass(juicontext));
                if (!localClassUIContext) {
                    break;
                }

                methodID_UIContext_close = env->GetMethodID(localClassUIContext,
                                                            METHOD_NAME_UICONTEXT_CLOSE,
                                                            METHOD_SIG_UICONTEXT_CLOSE);
            }

            if (!methodID_UIContext_close) {
                assert(0);
                break;
            }

            env->CallVoidMethod(juicontext, methodID_UIContext_close);

        } while (0);
    }

    jobject getRootView(jobject juicontext) {
        jobject juiview = nullptr;

        do {
            JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
            if (!env) {
                break;
            }

            if (!juicontext) {
                break;
            }

            if (methodID_UIContext_getRootView == nullptr) {
                ScopedLocalRef<jclass> localClassUIView(env, env->GetObjectClass(juicontext));
                if (!localClassUIView) {
                    break;
                }

                methodID_UIContext_getRootView = env->GetMethodID(localClassUIView,
                                                                  METHOD_NAME_UICONTEXT_GETROOTVIEW,
                                                                  METHOD_SIG_UICONTEXT_GETROOTVIEW);
            }

            if (!methodID_UIContext_getRootView) {
                assert(0);
                break;
            }

            juiview = env->CallObjectMethod(juicontext, methodID_UIContext_getRootView);

        } while (0);
        return juiview;
    }

    jobject findView(jobject juicontext, const std::string &id) {
        jobject juiview = nullptr;

        do {
            JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
            if (!env) {
                break;
            }

            if (!juicontext) {
                break;
            }

            if (methodID_UIContext_findView == nullptr) {
                ScopedLocalRef<jclass> localClassUIView(env, env->GetObjectClass(juicontext));
                if (!localClassUIView) {
                    break;
                }

                methodID_UIContext_findView = env->GetMethodID(localClassUIView,
                                                               METHOD_NAME_UICONTEXT_FINDVIEW,
                                                               METHOD_SIG_UICONTEXT_FINDVIEW);
            }

            if (!methodID_UIContext_findView) {
                assert(0);
                break;
            }

            ScopedLocalRef<jstring> jid(env, env->NewStringUTF(id.c_str()));
            if (!jid) {
                break;
            }

            juiview = env->CallObjectMethod(juicontext, methodID_UIContext_findView, (jstring) jid);

        } while (0);
        return juiview;
    }
};
